package com.lzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.dao.ProductDao;
import com.lzx.dto.ProductDto;
import com.lzx.entity.Category;
import com.lzx.entity.Pictrue;
import com.lzx.entity.Product;
import com.lzx.service.BrandService;
import com.lzx.service.CategoryService;
import com.lzx.service.PictrueService;
import com.lzx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author makejava
 * @since 2021-02-24 17:27:04
 */
@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, Product> implements ProductService {
    @Resource
    private ProductDao productDao;

    @Autowired
    private PictrueService pictrueService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Product queryById(Integer id) {
        return this.productDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Product> queryAllByLimit(int offset, int limit) {
        return this.productDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @Override
    public Product insert(Product product) {
        this.productDao.insert(product);
        return product;
    }

    /**
     * 修改数据
     *
     * @param product 实例对象
     * @return 实例对象
     */
    @Override
    public Product update(Product product) {
        this.productDao.update(product);
        return this.queryById(product.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.productDao.deleteById(id) > 0;
    }

    /**
     * 分页查询购物车数据
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public Page<Product> listPage(Page<Product> page, QueryWrapper wrapper) {
        Page<Product> productPage = productDao.selectPage(page, wrapper);
        return productPage;
    }

    /**
     * 获取分类下的商品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Product> queryByCategory(Integer categoryId) {
        return productDao.queryByCategory(categoryId);
    }

    /**
     * 获取推荐商品
     *
     * @return
     */
    @Override
    public List<ProductDto> getRecommend() {
        List<Product> products = productDao.queryRecommend();
        List<ProductDto> productDtos = new ArrayList<>();
        products.forEach(product -> {
            ProductDto dto = new ProductDto();
            BeanUtil.copyProperties(product,dto);
            productDtos.add(dto);
        });
        productDtos.forEach(productDto -> {
            Pictrue defaultImg = pictrueService.getDefaultImg("product", productDto.getId());
            productDto.setImg(defaultImg.getSrc());
        });
        if(BeanUtil.isNotEmpty(productDtos)){
            return productDtos;
        }
        return null;
    }

    /**
     * 更改商品状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Boolean changeStatus(Integer id, Boolean status) {
        Product product = productDao.selectById(id);
        product.setIsactive(status);
        int r = productDao.update(product);
        if(r>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 添加商品
     *
     * @param product
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean add(Product product) {
        int insert = productDao.insert(product);
        if(insert>0){
            product.getCategories().forEach(x ->{
                productDao.insertCategoriesForProduct(product.getId(),x);
            });
            return true;
        }
        return false;

    }

    /**
     * 更新Es的商品数据
     */
    @Override
    public void dbToEs() {
        List<Product> productList = this.list();
        List<ProductDto> productDtos = new ArrayList<>();
        productList.forEach(result->{
            ProductDto dto = new ProductDto();
            BeanUtil.copyProperties(result,dto);
            Pictrue defaultImg = pictrueService.getDefaultImg("product", result.getId());
            dto.setImg(defaultImg.getSrc());
            dto.setBrand(brandService.getById(result.getBrandid()).getName());
            List<Category> categories = categoryService.queryCatesByProductId(result.getId());
            dto.setCategories(categories);
            productDtos.add(dto);
        });
        restTemplate.save(productDtos);
    }

}
